﻿using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.Service.Model.Insur.Request;
using Ewell.BizComponent.Service.Model.Insur.Response;
using Ewell.BizComponent.Service.Model.InterProxy;
using Ewell.BizComponent.Service.Model.Request;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Ewell.BizComponent.Service.Server
{
    public class InsurServer : IInsurServer
    {
        const string interproxyProName = "bin\\plugin\\Ewell.Interface.Proxy.exe";
        /// <summary>
        /// 是否需要读卡
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        NeedReadCardResponse IInsurServer.GetNeedReadCard(RequestHeader header, NeedReadCardRequest request)
        {
            //接口调用参数
            header.tradeCode = "514";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            NeedReadCardResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<NeedReadCardResponse>();
            else
                result = (NeedReadCardResponse)clientResult?.@object;
            return result;
        }

        /// <summary>
        /// 选择项初始化
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        List<DataSelectItemResponse> IInsurServer.InitDataSelectItem(RequestHeader header, DataSelectItemInitRequest request)
        {
            //接口调用参数
            header.tradeCode = "511";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            List<DataSelectItemResponse> result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<List<DataSelectItemResponse>>();
            else if (clientResult.@object?.GetType() == typeof(JArray))
                result = ((JArray)clientResult.@object).ToObject<List<DataSelectItemResponse>>();
            else
                result = (List<DataSelectItemResponse>)clientResult?.@object;
            return result;
        }
        /// <summary>
        /// 交易数据选择项检索
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        List<BasicDataResponse> IInsurServer.SearchDataSelectItem(RequestHeader header, DataSelectItemQueryRequest request)
        {
            //接口调用参数
            header.tradeCode = "512";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            List<BasicDataResponse> result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<List<BasicDataResponse>>();
            else if (clientResult.@object?.GetType() == typeof(JArray))
                result = ((JArray)clientResult.@object).ToObject<List<BasicDataResponse>>();
            else
                result = (List<BasicDataResponse>)clientResult?.@object;
            return result;
        }
        /// <summary>
        /// 挂号预算
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="SpeedException"></exception>
        OutRegisterResponse IInsurServer.previewOutRegister(RequestHeader header, OutRegisterPreviewRequest request)
        {
            header.tradeCode = "101";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            Model.Insur.Response.OutRegisterResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<Model.Insur.Response.OutRegisterResponse>();
            else
                result = (Model.Insur.Response.OutRegisterResponse)clientResult?.@object;
            return result;
        }
        /// <summary>
        /// 门诊挂号预算取消
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <exception cref="SpeedException"></exception>
        void IInsurServer.cancelOutRegisterPreview(RequestHeader header, OutRegisterCancelPreviewRequest request)
        {
            header.tradeCode = "102";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            return;
        }
        /// <summary>
        /// 门诊挂号正算前
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        OutRegisterResponse IInsurServer.beforeOutRegister(RequestHeader header, OutRegisterBeforeRequest request)
        {
            header.tradeCode = "103";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            Model.Insur.Response.OutRegisterResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<Model.Insur.Response.OutRegisterResponse>();
            else
                result = (Model.Insur.Response.OutRegisterResponse)clientResult?.@object;
            return result;
        }

        /// <summary>
        /// 门诊挂号正算失败
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <exception cref="SpeedException"></exception>
        void IInsurServer.failOutRegister(RequestHeader header, OutRegisterFailRequest request)
        {
            header.tradeCode = "104";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            return;
        }

        /// <summary>
        /// 门诊挂号正算后
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <exception cref="SpeedException"></exception>
        void IInsurServer.afterOutRegister(RequestHeader header, OutRegisterAfterRequest request)
        {
            header.tradeCode = "105";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            return;
        }
        /// <summary>
        /// 门诊收费预算
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="SpeedException"></exception>
        OutPayResponse IInsurServer.previewOutPay(RequestHeader header, OutPayPreviewRequest request)
        {
            header.tradeCode = "201";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            Model.Insur.Response.OutPayResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<Model.Insur.Response.OutPayResponse>();
            else
                result = (Model.Insur.Response.OutPayResponse)clientResult?.@object;
            return result;
        }
        /// <summary>
        /// 门诊收费预算取消
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        void IInsurServer.cancelOutPayPreview(RequestHeader header, OutPayCancelPreviewRequest request)
        {
            header.tradeCode = "202";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            return;
        }
        /// <summary>
        /// 门诊收费正算前
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="SpeedException"></exception>
        OutPayResponse IInsurServer.beforeOutPay(RequestHeader header, OutPayBeforeRequest request)
        {
            header.tradeCode = "203";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            Model.Insur.Response.OutPayResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<Model.Insur.Response.OutPayResponse>();
            else
                result = (Model.Insur.Response.OutPayResponse)clientResult?.@object;
            return result;
        }
        /// <summary>
        /// 门诊收费正算失败
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        void IInsurServer.failOutPay(RequestHeader header, OutPayFailRequest request)
        {
            header.tradeCode = "204";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            return;
        }
        /// <summary>
        /// 门诊收费正算后
        /// </summary>
        /// <param name="header"></param>
        /// <param name="request"></param>
        /// <exception cref="SpeedException"></exception>
        void IInsurServer.afterOutPay(RequestHeader header, OutPayAfterRequest request)
        {
            header.tradeCode = "205";
            header.employeeNo = LoginInfo.loginUser.no;
            header.employeeName = LoginInfo.loginUser.userName;
            header.deptNo = LoginInfo.loginUser.deptCode;
            header.deptName = LoginInfo.loginUser.deptName;
            header.zone = LoginInfo.loginUser.zone;
            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(header),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            return;
        }
    }
}
